<?php

/**
 * 付款类型
 * File Name：MoneyPositionModel.php
 * File Encoding：UTF-8
 * File New Time：2014-5-14 10:50:28
 * Author：水平凡
 * Mailbox：admin@abc3210.com
 */
class MoneyPositionModel extends BaseModel {

    public function tableName() {
        return '{{money_position}}';
    }

    public function rules() {
        return array(
            array('title', 'required', 'message' => '标题不能为空！'),
            array('money', 'required', 'message' => '收费金额不能为空！'),
            array('start_time', 'required', 'message' => '开始时间不能为空！'),
            array('end_time', 'required', 'message' => '结束时间不能为空！'),
            array('callback', 'required', 'message' => '回调名不能为空！'),
            //安全的。。。
            array('type,status,template', 'safe'),
        );
    }

    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * 根据id获取名称
     * @param type $id 
     * @return boolean
     */
    public function getByIdName($id) {
        $data = $this->findByPk($id);
        if (empty($data)) {
            return false;
        }
        return $data->title;
    }

    /**
     * 获取全部的收费类型
     * @param type $isIdByName 是否 是 id=>title 的格式？否则直接返回整条数据
     * @return type
     */
    public function getPositionAll($isIdByName = false) {
        $All = array();
        $data = $this->findAll($this->where(array('order' => 'id desc')));
        foreach ($data as $rs) {
            if ($isIdByName) {
                $All[$rs->id] = $rs->title;
            } else {
                $All[$rs->id] = $rs->attributes;
            }
        }
        return $All;
    }

    public function getPositionInfo($id) {
        return $this->findByPk($id);
    }

    //数据保存前操作
    protected function beforeSave() {
        if ($this->start_time) {
            $this->start_time = strtotime($this->start_time);
        }
        if ($this->end_time) {
            $this->end_time = strtotime($this->end_time);
        }
        //是否新增
        if ($this->isNewRecord) {
            
        } else {
            
        }
        return true;
    }

}
